//
//  LNFocusTableViewCell.m
//  LNDevelopExample
//
//  Created by wwww on 2018/9/28.
//  Copyright © 2018年 淘发现1iOS. All rights reserved.
//

#import "LNFocusTableViewCell.h"
#import "MHCycleScrollView.h"
#import "UIImageView+LBBlurredImage.h"
#import "LNBanner.h"

@interface LNFocusTableViewCell()<MHCycleScrollViewDelegate>
// 轮播图
@property (nonatomic,readwrite,strong) MHCycleScrollView *cycleScrollView;
// 背景
@property (nonatomic,readwrite,strong) UIImageView *bgImaegView;
// 阴影
@property (nonatomic,readwrite,strong) UIView *shadowView;
// 分页
@property (nonatomic,readwrite,strong) UIPageControl *pageControl;
@end

@implementation LNFocusTableViewCell

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self setupUI];
    }
    return self;
}

/// viewModel - The view model
- (void)bindViewModel:(NSArray *)imgs{
    
    NSMutableArray *imgarray = [NSMutableArray array];
    for (LNBanner *banner in imgs) {
        [imgarray addObject:banner.img];
    }
    
    self.cycleScrollView.imageURLStringsGroup = imgarray;
    self.pageControl.numberOfPages = imgarray.count;
    if(imgarray.count>0){
        NSString *imageurl = imgarray[0];
        [self loadImageWithUrl:imageurl];
    }
}

#pragma mark - 初始化UI
-(void)setupUI{
    
    [self addSubview:self.bgImaegView];
    [self addSubview:self.pageControl];
    [self.bgImaegView addSubview:self.shadowView];
    [self.shadowView addSubview:self.cycleScrollView];
    
    [self.bgImaegView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(10);
        make.width.mas_equalTo(120);
        make.bottom.mas_equalTo(self).offset(-5);
    }];
    
    [self.shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgImaegView).offset(LN_APPLICATION_TOP_BAR_HEIGHT+20);
        make.left.mas_equalTo(self.bgImaegView).offset(20);
        make.bottom.right.mas_equalTo(self.bgImaegView).offset(-20);
    }];
    
    [self.cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.shadowView);
    }];
}

#pragma makr - MHCycleScrollViewDelegate
/** 点击图片回调 */
- (void)cycleScrollView:(MHCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
  //  [self.bannerSubject sendNext:@(index)];
}

- (void)cycleScrollView:(MHCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
    
    self.pageControl.currentPage = index;
    NSString *imageurl = cycleScrollView.imageURLStringsGroup[index];
    [self loadImageWithUrl:imageurl];
}

#pragma makr -下载图片设置背景图片
-(void)loadImageWithUrl:(NSString *)imageurl
{
    @weakify(self)
    [[YYWebImageManager sharedManager] requestImageWithURL:[NSURL URLWithString:imageurl] options:LNWebImageOptionAutomatic progress:nil transform:nil completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        @strongify(self)
        dispatch_async(dispatch_get_main_queue(), ^{
            //UI更新代码
            if(image != nil)
                [self.bgImaegView setImageToBlur:image blurRadius:20 completionBlock:nil];
        });
    }];
}

#pragma mark - setter and getter
-(MHCycleScrollView *)cycleScrollView{
    
    if (_cycleScrollView == nil) {
        _cycleScrollView = [MHCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImage imageNamed:@"ugc_image"]];
        //设置滚动方向
        _cycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        //--- 轮播时间间隔，默认2.0秒，可自定义时间间隔
        _cycleScrollView.autoScrollTimeInterval = 3.0;
        _cycleScrollView.currentPageDotColor = [UIColor clearColor];
        _cycleScrollView.pageDotColor = [UIColor clearColor];
        _cycleScrollView.userInteractionEnabled = YES;
        _cycleScrollView.layer.cornerRadius = 5.0;
        _cycleScrollView.layer.masksToBounds = YES;
    }
    return _cycleScrollView;
}

-(UIView *)shadowView{
    
    if(_shadowView == nil){
        
        _shadowView = [[UIView alloc]initWithFrame:CGRectZero];
        
        _shadowView.layer.shadowColor = [UIColor blackColor].CGColor;
        
        _shadowView.layer.shadowOpacity = 0.8;
        
        _shadowView.layer.shadowRadius = 10;
        
        _shadowView.clipsToBounds = NO;
    }
    return _shadowView;
    
}

-(UIImageView *)bgImaegView{
    
    if(_bgImaegView == nil){
        _bgImaegView = [[UIImageView alloc] initWithImage:[UIImage imageWithColor:LN_MAIN_TINTCOLOR size:CGSizeMake(LN_SCREEN_WIDTH, kHeight(183))]];
        _bgImaegView.userInteractionEnabled = YES;
        _bgImaegView.layer.masksToBounds = YES;
    }
    return _bgImaegView;
}

-(UIPageControl *)pageControl{
    
    if(_pageControl == nil){
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.currentPageIndicatorTintColor = LNColorFromHexString(@"#808080");
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.userInteractionEnabled = NO;
        _pageControl.currentPage = 0;
    }
    return _pageControl;
}

@end
